package cn.tedu.car.backend.customer.controller;

import cn.tedu.car.backend.base.response.JsonResult;
import cn.tedu.car.backend.base.response.PageData;
import cn.tedu.car.backend.customer.pojo.dto.CustomerAddDto;
import cn.tedu.car.backend.customer.pojo.dto.CustomerQueryParam;
import cn.tedu.car.backend.customer.pojo.dto.CustomerRegDto;
import cn.tedu.car.backend.customer.pojo.dto.IdentifyDto;
import cn.tedu.car.backend.customer.pojo.vo.CustomerDetailVo;
import cn.tedu.car.backend.customer.pojo.vo.CustomerListVo;
import cn.tedu.car.backend.customer.pojo.vo.CustomerLoginInfoVO;
import cn.tedu.car.backend.customer.pojo.vo.IdentifyResultVO;
import cn.tedu.car.backend.customer.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author LLY
 */
@Slf4j
@RestController
@Api(tags = "客户管理")
@RequestMapping("/v1/customer")
public class CustomerController {
    CustomerController() {
        log.debug("初始化客户管理控制器： CustomerController");
    }

    @Autowired
    private CustomerService customerService;

    @PostMapping("add")
    @ApiOperation(value = "注册客户")
    public JsonResult add(CustomerAddDto customerAddDto) {
        log.debug("新增客户，{}", customerAddDto);
        customerService.add(customerAddDto);
        return JsonResult.ok();
    }


    @GetMapping("{id}/ban")
    @ApiOperation(value = "禁用/启用客户")
    public JsonResult ban(@PathVariable("id") Long id) {
        log.debug("禁用/启用客户，id=" + id);
        customerService.changeStateById(id);
        return JsonResult.ok();
    }


    @GetMapping("{id}/detail")
    @ApiOperation(value = "获取客户详情")
    public JsonResult getCustomerById(@PathVariable("id") Long id) {
        log.debug("获取客户详情，id=" + id);
        CustomerDetailVo customerListVo = customerService.getCustomerById(id);
        return JsonResult.ok(customerListVo);
    }

    @GetMapping("list")
    @ApiOperation(value = "获取客户列表")
    public JsonResult list(CustomerQueryParam param) {
        log.debug("获取客户列表，{}", param);
        PageData list = customerService.list(param);
        return JsonResult.ok(list);
    }

    @GetMapping("login")
    @ApiOperation(value = "登录")
    public JsonResult login(String username, String password) {
        log.debug("登录，username=" + username + ", password=" + password);
        CustomerLoginInfoVO customerLoginInfoVO = customerService.login(username, password);
        return JsonResult.ok(customerLoginInfoVO);
    }

    @PostMapping("register")
    @ApiOperation(value = "注册")
    public JsonResult register(CustomerRegDto customerRegDto) {
        log.debug("注册，customerRegDto=" + customerRegDto);
        customerService.register(customerRegDto);
        return JsonResult.ok();
    }

    @ApiOperation("发送验证码")
    @GetMapping("/send/{phone}")
    public JsonResult sendMessage(@PathVariable String phone) {
        log.debug("开始处理【发送验证码】的请求，参数{}", phone);
        customerService.sendCaptcha(phone);
        return JsonResult.ok("短信发送成功");
    }

    @ApiOperation("使用手机号验证码登录")
    @PostMapping("/login/phone")
    public JsonResult loginWithPhone(String phone, String code) {
        log.debug("开始处理【手机号验证码登录】的请求，参数{}", phone);
        customerService.loginWithPhone(phone, code);
        return JsonResult.ok("短信发送成功");
    }

    @ApiOperation("实名认证")
    @PostMapping("/identify")
    public JsonResult identify(IdentifyDto identifyDto) throws IOException {
        customerService.identify(identifyDto);
        return JsonResult.ok();
    }
}
